package cn.cj.erp.modules.system.controller;

import cn.cj.erp.bo.system.SysDeptBo;
import cn.cj.erp.common.vo.TreeNode;
import cn.cj.erp.modules.common.controller.AbstractController;
import cn.cj.erp.service.DeptService;
import cn.cj.erp.service.system.SysDeptService;
import cn.hutool.core.util.ArrayUtil;
import cn.cj.erp.common.vo.RoleDeptVo;
import cn.cj.erp.common.security.annotation.Log;
import cn.cj.erp.common.enums.LogBusinessTypeEnum;
import cn.cj.erp.common.http.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * 部门管理
 *
 * @author chenjian
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends AbstractController {
    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private DeptService deptService;
    
    /**
     * 列表
     */
    @PostMapping("/queryTree")
    @PreAuthorize("@ss.hasPerm('system:dept:list')")
    public Result<List<SysDeptBo>> queryTree(@RequestBody SysDeptBo params) {
        return Result.query(sysDeptService.queryTree(params));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPerm('system:dept:list')")
    @PostMapping("/deptTree")
    public Result<List<TreeNode>> deptTree(@RequestBody SysDeptBo params) {
        return Result.query(deptService.queryTreeNode(params));
    }

    /**
     * 获取当前父节点下最大编号
     * @param parentId 父节ID
     * @return Result
     */
    @PreAuthorize("@ss.hasPerm('system:dept:query')")
    @GetMapping("/getCurrentUsedOrderNum/{parentId}")
    public Result<Integer> getCurrentUsedOrderNum(@PathVariable("parentId") Long parentId) {
        return Result.query(sysDeptService.getCurrentUsedOrderNum(parentId));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPerm('system:dept:query')")
    public Result<SysDeptBo> info(@PathVariable("id") Long id) {
        return Result.query(sysDeptService.getById(id));
    }

    /**
     * 根据id列表获取详情
     */
    @PostMapping("/listByIds")
    @PreAuthorize("@ss.hasPerm('system:dept:list')")
    public Result<List<SysDeptBo>> listByIds(@RequestBody Long[] ids) {
        return Result.query(sysDeptService.listByIds(Arrays.asList(ids)));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('system:dept:add')")
    @Log(title = "部门管理", businessType = LogBusinessTypeEnum.INSERT)
    public Result<Boolean> save(@Valid @RequestBody SysDeptBo sysDept) {
        return Result.save(sysDeptService.save(sysDept));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('system:dept:edit')")
    @Log(title = "部门管理", businessType = LogBusinessTypeEnum.UPDATE)
    public Result<Boolean> update(@Valid @RequestBody SysDeptBo sysDept) {
        return Result.update(sysDeptService.updateById(sysDept));
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{deptIds}")
    @PreAuthorize("@ss.hasPerm('system:dept:remove')")
    @Log(title = "部门管理", businessType = LogBusinessTypeEnum.DELETE)
    public Result<Boolean> delete(@PathVariable("deptIds") Long[] deptIds) {
        if (ArrayUtil.isEmpty(deptIds) || deptIds.length > 1) {
            return Result.error("只能选择一个部门删除");
        }
        return Result.remove(sysDeptService.removeById(deptIds[0]));
    }

    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTree/{roleId}")
    public Result<RoleDeptVo> roleDeptTree(@PathVariable("roleId") Long roleId) {
        return Result.ok(deptService.roleDeptTree(roleId));
    }
}
